package com.zmn.oms.dubbo.impl.zmn.master.order;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.business.interfaces.orderattachment.OrderAttachmentBService;
import com.zmn.oms.common.dio.attachment.ModifyOrderWorkAttachmentDIO;
import com.zmn.oms.common.dio.attachment.OrderWorkAttachmentDIO;
import com.zmn.oms.common.dro.work.attachment.OrderWorkAttachmentDRO;
import com.zmn.oms.dubbo.interfaces.zmn.master.order.MasterWorkAttachmentListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.orderattachment.ModifyOrderAttachmentDTO;
import com.zmn.oms.model.entity.orderattachment.OrderAttachmentQuery;
import com.zmn.oms.model.vo.conf.attachment.ConfOrderAttachmentVO;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.attachmen.OrderWorkAttachmentVO;
import com.zmn.oms.services.interfaces.conf.attachment.ConfOrderAttachmentService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;

/**
 * 类描述：师傅工单附件
 *
 * 因迁移贴花照数据，需要一个符合新需求的工单附件类，保留本类为了解决老数据兼容问题。
 * @author liuying
 * @date 2021/06/11 15:36
 * @see com.zmn.oms.dubbo.impl.zmn.master.order.MasterOrderAttachmentListRemoteServiceImpl
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Deprecated
public class MasterWorkAttachmentListRemoteServiceImpl implements MasterWorkAttachmentListRemoteService {

    @Autowired
    private OrderAttachmentBService orderAttachmentBService;
    @Autowired
    private ZsOrderWorkOperatePermissionBService zsOrderWorkOperatePermissionBService;
    @Autowired
    private ConfOrderAttachmentService confOrderAttachmentService;

    @Override
    public ResponseDTO<OrderWorkAttachmentDRO> listOrderWorkAttachmentByQuery(@NotNull(message = "入参不能为空") OrderWorkAttachmentDIO orderWorkAttachmentDIO) {
        try {
            log.info("（old）查询工单附件 入参：[{}]", orderWorkAttachmentDIO);
            OrderAttachmentQuery orderAttachmentQuery = BeanMapper.map(orderWorkAttachmentDIO, OrderAttachmentQuery.class);
            OrderWorkAttachmentVO orderWorkAttachmentVO = orderAttachmentBService.getOrderAttachmentByQuery(orderAttachmentQuery);
            OrderWorkAttachmentDRO orderWorkAttachmentDRO = BeanMapper.map(orderWorkAttachmentVO, OrderWorkAttachmentDRO.class);
            log.info("（old ）查询工单附件 出参：{}", JSONObject.toJSONString(orderWorkAttachmentDRO));
            return ResponseDTO.success(orderWorkAttachmentDRO);
        } catch (Exception exception) {
            log.error(exception.getMessage(), exception);
            return ResponseDTO.fail("查询工单附件失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO getConfOrderAttachmentByKey(Integer confAttachmentId) {
        try {
            log.info("MasterWorkAttachmentListRemoteService#listOrderWorkAttachmentByQuery confAttachmentId={}", confAttachmentId);
            ConfOrderAttachmentVO confOrderAttachmentVO = confOrderAttachmentService.findConfById(confAttachmentId);
            log.info("MasterWorkAttachmentListRemoteService#listOrderWorkAttachmentByQuery orderWorkAttachmentDRO={}", confOrderAttachmentVO);
            return ResponseDTO.success(confOrderAttachmentVO);
        } catch (Exception exception) {
            log.error(exception.getMessage(), exception);
            return ResponseDTO.fail("获取工单附件配置失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO modifyOrderWorkAttachment(ModifyOrderWorkAttachmentDIO modifyOrderWorkAttachmentDIO) {
        try {
            log.info("（old）上传工单照片 入参：[{}]", modifyOrderWorkAttachmentDIO);
            ModifyOrderAttachmentDTO modifyOrderAttachmentDTO = BeanMapper.map(modifyOrderWorkAttachmentDIO, ModifyOrderAttachmentDTO.class);
            modifyOrderAttachmentDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
            modifyOrderAttachmentDTO.setOperatorId((long)modifyOrderWorkAttachmentDIO.getMasterId());
            orderAttachmentBService.modifyOrderWorkAttachment(modifyOrderAttachmentDTO);
            return ResponseDTO.success();
        } catch (Exception ex) {
            log.error("MasterWorkAttachmentListRemoteService#modifyOrderWorkAttachment", ex);
            return ResponseDTO.fail("保存工单附件失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO requireOrderChannelAttachment(@NotNull(message = "入参不能为空") OrderWorkAttachmentDIO orderWorkAttachmentDIO) {
        try {
            log.info("MasterWorkAttachmentListRemoteService#requireOrderChannelAttachment orderWorkAttachmentDIO={}", orderWorkAttachmentDIO);
            OrderAttachmentQuery orderAttachmentQuery = BeanMapper.map(orderWorkAttachmentDIO, OrderAttachmentQuery.class);
            OperatePermissionVO operatePermissionVO = zsOrderWorkOperatePermissionBService.requireOrderChannelAttachment(orderAttachmentQuery);
            log.info("MasterWorkAttachmentListRemoteService#requireOrderChannelAttachment operatePermissionVO={}", operatePermissionVO);
            if (operatePermissionVO.getCanOperate()) {
                return ResponseDTO.success();
            }
            return ResponseDTO.fail(operatePermissionVO.getMsg());
        } catch (Exception exception) {
            return ResponseDTO.fail("查询工单附件失败，请稍后重试");
        }
    }

}
